package de.lmu.mvs.lerngruppenfinder;

import java.util.HashMap;

import com.eclipsesource.json.JsonObject;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import de.lmu.mvs.lerngruppenfinder.utils.ApplicationController;
import de.lmu.mvs.lerngruppenfinder.utils.Constants;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class PositionSelectActivity extends FragmentActivity {

	private GoogleMap mMap;

	private HashMap<Marker, JsonObject> mMarkerMap = new HashMap<Marker, JsonObject>();

	private double latitude = 48.138675;
	private double longitude = 11.57774;

	private Marker marker;
	private LatLng ltg;

	private boolean mTransitionStateSave = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		getActionBar().hide();

		if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}

		setContentView(R.layout.activity_position_select);

		mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.position_map)).getMap();

		if(mMap != null){
			mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 13.0f));

			((TextView)findViewById(R.id.user_interaction_save_changes))
			.setTypeface(ApplicationController.getTypeFace());
			((TextView)findViewById(R.id.user_interaction_cancel))
			.setTypeface(ApplicationController.getTypeFace());

			mMap.setOnMapClickListener(new OnMapClickListener(){

				@Override
				public void onMapClick(LatLng arg0) {
					ltg = new LatLng(arg0.latitude, arg0.longitude);

					mMap.clear();
					marker = setMarker(ltg, "Location");

					if(ltg != null){
						transitionToSave();
					}
				}});
		}
		
		findViewById(R.id.user_interaction_save_changes).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent resultIntent = new Intent();
				resultIntent.putExtra("Gpscoords", ltg );
				setResult(Activity.RESULT_OK, resultIntent);
				finish();
			}
		});

		findViewById(R.id.user_interaction_cancel).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		
		findViewById(R.id.user_interaction_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private Marker setMarker(LatLng location, String name) {
		Marker marker = mMap.addMarker(
				new MarkerOptions()
				.position(location));
		return marker;
	}

	private void transitionToSave(){
		((TextView)findViewById(R.id.user_interaction_title)).setText(R.string.user_interaction_save_position);
		findViewById(R.id.user_interaction_save_changes).setVisibility(View.VISIBLE);
		findViewById(R.id.user_interaction_cancel).setVisibility(View.GONE);

		if(!mTransitionStateSave){
			((TransitionDrawable) findViewById(R.id.user_interaction)
					.getBackground()).startTransition(250);
		}

		mTransitionStateSave = true;
	}

	private void transitionToCancel(){
		((TextView)findViewById(R.id.user_interaction_title)).setText(R.string.user_interaction_choose_position);
		findViewById(R.id.user_interaction_save_changes).setVisibility(View.GONE);
		findViewById(R.id.user_interaction_cancel).setVisibility(View.VISIBLE);

		if(mTransitionStateSave){
			((TransitionDrawable) findViewById(R.id.user_interaction)
					.getBackground()).reverseTransition(250);
		}

		mTransitionStateSave = false;
	}
}
